﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace SinacorWebTools.Web
{
    public class PagingGridView : GridView
    {
        public enum PagerTypes
        {
            Regular = 0,
            Custom = 1
        }

        [DefaultValue(PagerTypes.Custom), Category("Paging"), Description("Indicates whether to use the built-in custom pager or not.")]
        public PagerTypes PagerType
        {
            get
            {
                if ((ViewState["PagerType"] != null))
                    return (PagerTypes)ViewState["PagerType"];
                else
                    return PagerTypes.Custom;
            }
            set { ViewState["PagerType"] = value; }
        }

        protected override void InitializePager(System.Web.UI.WebControls.GridViewRow row, int columnSpan, System.Web.UI.WebControls.PagedDataSource pagedDataSource)
        {
            switch (this.PagerType)
            {
                case PagerTypes.Custom:
                    InitCustomPager(row, columnSpan, pagedDataSource);
                    break;
                default:
                    base.InitializePager(row, columnSpan, pagedDataSource);
                    break;
            }
        }

        private void InitCustomPager(System.Web.UI.WebControls.GridViewRow row, int columnSpan, System.Web.UI.WebControls.PagedDataSource pagedDataSource)
        {
            Panel pnlPager = new Panel();
            var _with1 = pnlPager;
            _with1.ID = "pnlPager";
            _with1.CssClass = this.PagerStyle.CssClass;

            Table tblPager = new Table();
            var _with2 = tblPager;
            _with2.ID = "tblPager";
            _with2.CellPadding = 3;
            _with2.CellSpacing = 0;
            _with2.Style.Add("width", "100%");
            _with2.Style.Add("height", "100%");
            _with2.BorderStyle = BorderStyle.None;
            _with2.GridLines = GridLines.None;

            TableRow trPager = new TableRow();
            trPager.ID = "trPager";

            Literal ltlPageIndex = new Literal();
            ltlPageIndex.ID = "ltlPageIndex";
            ltlPageIndex.Text = (this.PageIndex + 1).ToString();

            Literal ltlPageCount = new Literal();
            ltlPageCount.ID = "ltlPageCount";
            ltlPageCount.Text = this.PageCount.ToString();

            TableCell tcPageXofY = new TableCell();
            var _with3 = tcPageXofY;
            _with3.ID = "tcPageXofY";
            _with3.Style.Add("width", "30%");
            _with3.Style.Add("text-align", "left");
            _with3.Style.Add("padding-left", "5px");
            _with3.Style.Add("border", "0px");
            _with3.Controls.Add(new LiteralControl("Página "));
            _with3.Controls.Add(ltlPageIndex);
            _with3.Controls.Add(new LiteralControl(" de "));
            _with3.Controls.Add(ltlPageCount);

            ImageButton ibtnFirst = new ImageButton();
            var _with4 = ibtnFirst;
            _with4.ID = "ibtnFirst";
            _with4.CommandName = "First";
            _with4.ToolTip = "Primeira Página";
            _with4.ImageAlign = ImageAlign.AbsMiddle;
            _with4.Style.Add("cursor", "pointer");
            _with4.CausesValidation = false;
            _with4.Command += this.PagerCommand;

            ImageButton ibtnPrevious = new ImageButton();
            var _with5 = ibtnPrevious;
            _with5.ID = "ibtnPrevious";
            _with5.CommandName = "Previous";
            _with5.ToolTip = "Página Anterior";
            _with5.ImageAlign = ImageAlign.AbsMiddle;
            _with5.Style.Add("cursor", "pointer");
            _with5.CausesValidation = false;
            _with5.Command += this.PagerCommand;

            ImageButton ibtnNext = new ImageButton();
            var _with6 = ibtnNext;
            _with6.ID = "ibtnNext";
            _with6.CommandName = "Next";
            _with6.ToolTip = "Próxima Página";
            _with6.ImageAlign = ImageAlign.AbsMiddle;
            _with6.Style.Add("cursor", "pointer");
            _with6.CausesValidation = false;
            _with6.Command += this.PagerCommand;

            ImageButton ibtnLast = new ImageButton();
            var _with7 = ibtnLast;
            _with7.ID = "ibtnLast";
            _with7.CommandName = "Last";
            _with7.ToolTip = "Última Página";
            _with7.ImageAlign = ImageAlign.AbsMiddle;
            _with7.Style.Add("cursor", "pointer");
            _with7.CausesValidation = false;
            _with7.Command += this.PagerCommand;

            string url = "~/images/grid/";
            if (this.PageIndex > 0)
            {
                ibtnFirst.ImageUrl = url + "page-first.gif";
                ibtnPrevious.ImageUrl = url + "page-prev.gif";
                ibtnFirst.Enabled = true;
                ibtnPrevious.Enabled = true;
            }
            else
            {
                ibtnFirst.ImageUrl = url + "page-first-disabled.gif";
                ibtnPrevious.ImageUrl = url + "page-prev-disabled.gif";
                ibtnFirst.Enabled = false;
                ibtnPrevious.Enabled = false;
                ibtnFirst.Style.Add("cursor", "default");
                ibtnPrevious.Style.Add("cursor", "default");
            }

            if (this.PageIndex < this.PageCount - 1)
            {
                ibtnNext.ImageUrl = url + "page-next.gif";
                ibtnLast.ImageUrl = url + "page-last.gif";
                ibtnNext.Enabled = true;
                ibtnLast.Enabled = true;
            }
            else
            {
                ibtnNext.ImageUrl = url + "page-next-disabled.gif";
                ibtnLast.ImageUrl = url + "page-last-disabled.gif";
                ibtnNext.Enabled = false;
                ibtnLast.Enabled = false;
                ibtnNext.Style.Add("cursor", "default");
                ibtnLast.Style.Add("cursor", "default");
            }

            TableCell tcPagerBtns = new TableCell();
            var _with8 = tcPagerBtns;
            _with8.ID = "tcPagerBtns";
            _with8.Style.Add("width", "40%");
            _with8.Style.Add("text-align", "center");
            _with8.Style.Add("border", "0px");
            _with8.Controls.Add(ibtnFirst);
            _with8.Controls.Add(ibtnPrevious);
            _with8.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            _with8.Controls.Add(ibtnNext);
            _with8.Controls.Add(ibtnLast);

            DropDownList ddlPages = new DropDownList();
            var _with9 = ddlPages;
            _with9.ID = "ddlPages";
            _with9.CssClass = "paging_gridview_pgr_ddl";
            _with9.AutoPostBack = true;

            for (int i = 1; i <= this.PageCount; i += +1)
                _with9.Items.Add(new ListItem(i.ToString(), i.ToString()));

            _with9.SelectedIndex = this.PageIndex;
            _with9.CausesValidation = false;
            _with9.SelectedIndexChanged += this.ddlPages_SelectedIndexChanged;

            TableCell tcPagerDDL = new TableCell();
            var _with10 = tcPagerDDL;
            _with10.ID = "tcPagerDDL";
            _with10.Style.Add("width", "30%");
            _with10.Style.Add("text-align", "right");
            _with10.Style.Add("padding-right", "5px");
            _with10.Style.Add("border", "0px");
            _with10.Controls.Add(new LiteralControl("<table cellpadding=\"0\" cellspacing=\"0\" frame=\"void\" rules=\"none\"><tr><td style=\"padding-right: 5px;border: 0px;\">Página:</td><td style=\"border: 0px; color: White;\">"));
            _with10.Controls.Add(ddlPages);
            _with10.Controls.Add(new LiteralControl("</td></tr></table>"));

            //add cells to row
            trPager.Cells.Add(tcPageXofY);
            trPager.Cells.Add(tcPagerBtns);
            trPager.Cells.Add(tcPagerDDL);

            //add row to table
            tblPager.Rows.Add(trPager);

            //add table to div
            pnlPager.Controls.Add(tblPager);

            //add div to pager row
            row.Controls.AddAt(0, new TableCell());
            row.Cells[0].ColumnSpan = columnSpan;
            row.Cells[0].Controls.Add(pnlPager);
        }

        protected void ddlPages_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int newPageIndex = ((DropDownList)sender).SelectedIndex;
            OnPageIndexChanging(new GridViewPageEventArgs(newPageIndex));
        }

        protected void PagerCommand(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            int curPageIndex = this.PageIndex;
            int newPageIndex = 0;

            switch (e.CommandName)
            {
                case "First":
                    newPageIndex = 0;
                    break;
                case "Previous":
                    if (curPageIndex > 0)
                        newPageIndex = curPageIndex - 1;
                    break;
                case "Next":
                    if (!(curPageIndex == this.PageCount))
                        newPageIndex = curPageIndex + 1;
                    break;
                case "Last":
                    newPageIndex = this.PageCount;
                    break;
            }
            OnPageIndexChanging(new GridViewPageEventArgs(newPageIndex));
        }
    }
}